package com.scs.application.modules.base.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.scs.application.core.exception.BusinessException;
import com.scs.application.core.service.BaseServiceImpl;
import com.scs.application.core.utils.StringUtils;
import com.scs.application.modules.base.entity.DeptAddress;
import com.scs.application.modules.base.mapper.DeptAddressMapper;
import com.scs.application.modules.base.service.DeptAddressService;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 科室科室地址表 服务实现类
 * </p>
 *
 */
@Service
public class DeptAddressServiceImpl extends BaseServiceImpl<DeptAddressMapper, DeptAddress> implements DeptAddressService {
    @Override
    public boolean saveOrUpdate(DeptAddress entity) {
        if(entity.getFlagDefault() != null && entity.getFlagDefault()) { //同一科室只能有一个默认地址
            if(super.list(new QueryWrapper<DeptAddress>()
                    .eq("dept_id", entity.getDeptId())
                    .ne( StringUtils.isNotBlank(entity.getId()),"id",entity.getId())
                    .eq("flag_default", "1")).size() > 0) {
                throw new BusinessException("同一科室只能有一个默认地址");
            }
        }
        return super.saveOrUpdate(entity);
    }
}
